package com.theima.socket.socketdemo8;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @program: Myset
 * @description:
 * @author: xuejingjing
 * @create: 2021-06-26 15:59
 **/
public class ServerDemo {
    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(10000);
        //接受客户端传过来的数据
        Socket accept = ss.accept();
        //接受到之后，（从网络中读）
        InputStream inputStream = accept.getInputStream();

        //包装下，用于加速效率
        BufferedInputStream bis = new BufferedInputStream(inputStream);

        //需要写到服务器的本地电脑,也就是内存到本地
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copy.png"));


        int b;
        while((b = bis.read())!= -1){
            bos.write(b);
        }

        //告诉客户端文件已经上传，（也是在网络中传输）
       //服务器端输出信息（也就是返回给客户端）,这是字节流
         OutputStream outputStream = accept.getOutputStream();

        //转换流.字节流转换为字符流
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
        //转换为高效的字符流
        BufferedWriter bw = new BufferedWriter(outputStreamWriter);
        bw.write("上传成功");
        bw.newLine();
        bw.flush();
        //
        accept.close();
        ss.close();
        bos.close();
    }
}
